package com.shanshan.manage.web.service.impl;

import java.util.List;

import com.shanshan.manage.web.domain.ACustomsMsg;
import com.shanshan.manage.web.mapper.ACustomsMsgMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.shanshan.manage.web.mapper.AInventoryMapper;
import com.shanshan.manage.web.domain.AInventory;
import com.shanshan.manage.web.service.IAInventoryService;

import javax.annotation.Resource;

/**
 * 库存管理Service业务层处理
 *
 * @author zhaoshuheng
 * @date 2023-09-04
 */
@Service
public class AInventoryServiceImpl implements IAInventoryService {
    @Resource
    private AInventoryMapper aInventoryMapper;
    @Resource
    private ACustomsMsgMapper aCustomsMsgMapper;

    /**
     * 查询库存管理
     *
     * @param inventoryId 库存管理主键
     * @return 库存管理
     */
    @Override
    public AInventory selectAInventoryByInventoryId(Long inventoryId) {
        return aInventoryMapper.selectAInventoryByInventoryId(inventoryId);
    }

    /**
     * 查询库存管理列表
     *
     * @param aInventory 库存管理
     * @return 库存管理
     */
    @Override
    public List<AInventory> selectAInventoryList(AInventory aInventory) {
        return aInventoryMapper.selectAInventoryList(aInventory);
    }

    /**
     * 新增库存管理
     *
     * @param aInventory 库存管理
     * @return 结果
     */
    @Override
    public int insertAInventory(AInventory aInventory) {
        Long customsId = aInventory.getCustomsId();
        ACustomsMsg aCustomsMsg = aCustomsMsgMapper.selectACustomsMsgByCustomsId(customsId);
        long remainingQuantity = Long.parseLong(aCustomsMsg.getLowPieces()) - Long.parseLong(aInventory.getPieces());
        Double grossWeight = aCustomsMsg.getOneGrossWeight();//单件毛重
        Double netWeight = aCustomsMsg.getOneNetWeight();//单件净重
        Double lowGrossWeight = aCustomsMsg.getLowGrossWeight();//剩余毛重
        Double lowNetWeight = aCustomsMsg.getLowNetWeight();//剩余净重
        //新增出库信息
        aInventory.setShipName(aCustomsMsg.getShipName());//船名
        aInventory.setFlight(aCustomsMsg.getFlight());//航次
        aInventory.setSurplus(String.valueOf(remainingQuantity));//余量
        aInventory.setInventoryNum(aCustomsMsg.getInventoryNum());//核注清单号
        double maozhong = (double) Math.round(Double.parseDouble(aInventory.getPieces()) * grossWeight*1000)/1000;
        aInventory.setGrossWeight(maozhong);//毛重
        double jingzhong = (double) Math.round(Double.parseDouble(aInventory.getPieces()) * netWeight*1000)/1000;
        aInventory.setNetWeight(jingzhong);//净重
        //修改报关单的剩余库存信息
        if (remainingQuantity == 0) {
            aCustomsMsg.setLowGrossWeight(0.00);
            aCustomsMsg.setLowNetWeight(0.00);
            aCustomsMsg.setLowPieces(String.valueOf(remainingQuantity));
        } else {
            aCustomsMsg.setLowGrossWeight(lowGrossWeight - maozhong);
            aCustomsMsg.setLowNetWeight(lowNetWeight - jingzhong);
            aCustomsMsg.setLowPieces(String.valueOf(remainingQuantity));
        }
        aCustomsMsgMapper.updateACustomsMsg(aCustomsMsg);
        return aInventoryMapper.insertAInventory(aInventory);
    }

    /**
     * 修改库存管理
     *
     * @param aInventory 库存管理
     * @return 结果
     */
    @Override
    public int updateAInventory(AInventory aInventory) {
        return aInventoryMapper.updateAInventory(aInventory);
    }

    /**
     * 批量删除库存管理
     *
     * @param inventoryIds 需要删除的库存管理主键
     * @return 结果
     */
    @Override
    public int deleteAInventoryByInventoryIds(Long[] inventoryIds) {
        AInventory aInventory = aInventoryMapper.selectAInventoryByInventoryId(inventoryIds[0]);
        Double grossWeight = aInventory.getGrossWeight();
        Double netWeight = aInventory.getNetWeight();
        String pieces = aInventory.getPieces();
        Long customsId = aInventory.getCustomsId();
        ACustomsMsg aCustomsMsg = aCustomsMsgMapper.selectACustomsMsgByCustomsId(customsId);
        aCustomsMsg.setLowPieces(String.valueOf(Long.parseLong(aCustomsMsg.getLowPieces()) + Long.parseLong(pieces)));
        aCustomsMsg.setLowGrossWeight(aCustomsMsg.getLowGrossWeight() + grossWeight);
        aCustomsMsg.setLowNetWeight(aCustomsMsg.getLowNetWeight() + netWeight);
        aCustomsMsgMapper.updateACustomsMsg(aCustomsMsg);
        return aInventoryMapper.deleteAInventoryByInventoryIds(inventoryIds);
    }

    /**
     * 删除库存管理信息
     *
     * @param inventoryId 库存管理主键
     * @return 结果
     */
    @Override
    public int deleteAInventoryByInventoryId(Long inventoryId) {
        return aInventoryMapper.deleteAInventoryByInventoryId(inventoryId);
    }
}
